package org.vehicle.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.vehicle.dao.AlertInfoMapper;
import org.vehicle.dao.BlacklistMapper;
import org.vehicle.dao.CheckRecordMapper;
import org.vehicle.dao.DataRecordInfoMapper;
import org.vehicle.dao.OrganizationInfoMapper;
import org.vehicle.dao.PersonInfoMapper;
import org.vehicle.dao.PositionInfoMapper;
import org.vehicle.dao.TripInfoMapper;
import org.vehicle.dao.VehicleInfoMapper;
import org.vehicle.entity.AlertInfo;
import org.vehicle.entity.Blacklist;
import org.vehicle.entity.CheckRecord;
import org.vehicle.entity.DataRecordInfo;
import org.vehicle.entity.ManagerInfo;
import org.vehicle.entity.OrganizationInfo;
import org.vehicle.entity.PersonInfo;
import org.vehicle.entity.PositionInfo;
import org.vehicle.entity.TripInfo;
import org.vehicle.entity.VehicleInfo;
import org.vehicle.service.PersonInfoService;
import org.vehicle.service.base.BaseServiceImpl;
import org.vehicle.util.CommonUtil;
import org.vehicle.util.FtpUtil;
import org.vehicle.util.HttpClientUtil;
import org.vehicle.util.JsonUtils;
import org.vehicle.vo.CheckPersonInfo;

import com.fasterxml.jackson.databind.JsonNode;

@Service
public class PersonInfoServiceImpl extends BaseServiceImpl<PersonInfo, Long> implements PersonInfoService{
	
	private static Logger logger = LogManager.getLogger(PersonInfoServiceImpl.class);
	
	@Autowired
	private PersonInfoMapper personInfoMapper;
	@Autowired
	private DataRecordInfoMapper dataRecordInfoMapper;
	@Autowired
	private VehicleInfoMapper vehicleInfoMapper;
	@Autowired
	private TripInfoMapper tripInfoMapper;
	@Autowired
	private PositionInfoMapper positionInfoMapper;
	@Autowired
	private OrganizationInfoMapper organizationInfoMapper;
	@Autowired
	private BlacklistMapper blacklistMapper;
	@Autowired
	private AlertInfoMapper alertInfoMapper;
	@Autowired
	private CheckRecordMapper checkRecordMapper;
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public AlertInfo addPersonInfo(PersonInfo personInfo, VehicleInfo vehicleInfo,int checkType,ManagerInfo managerInfo,String eventId) {
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("vehicleInfoId", vehicleInfo.getVehicleInfoId());
		boolean addRecordFlag=false;
		if(checkType==3){
			personInfo.setVehicleInfoId(vehicleInfo.getVehicleInfoId());
			personInfo.setPersonType(3);
			personInfoMapper.insertSelective(personInfo);
			paramMap.put("checkType", 3);
			vehicleInfoMapper.updateCountById(paramMap);
			addRecordFlag=true;
		}else{
			PersonInfo tempPersonInfo=personInfoMapper.findPersonInfoByCertificateCode(personInfo.getCertificateCode());
			//判断人员是否存在
			if(tempPersonInfo==null){
				personInfo.setVehicleInfoId(vehicleInfo.getVehicleInfoId());
				personInfoMapper.insertSelective(personInfo);
				addRecordFlag=true;
			}else{
				//判断人员是否换车
				if(!tempPersonInfo.getVehicleInfoId().equals(vehicleInfo.getVehicleInfoId())){
					personInfo.setPersonInfoId(tempPersonInfo.getPersonInfoId());
					personInfo.setVehicleInfoId(vehicleInfo.getVehicleInfoId());
					personInfoMapper.updateByPrimaryKeySelective(personInfo);
					addRecordFlag=true;
				}
			}
		}
		//判断是否要新增历史记录
		if(!addRecordFlag){
			Map<String,Object> DataRecordParamMap=new HashMap<String,Object>();
			DataRecordParamMap.put("certificateCode", personInfo.getCertificateCode());
			DataRecordParamMap.put("organizationInfoId", personInfo.getOrganizationInfoId());
			DataRecordInfo tempDataRecordInfo=dataRecordInfoMapper.getDataRecordInfoByOrgIdAndCertificateCode(DataRecordParamMap);
			if(tempDataRecordInfo!=null){
				return null;
			}
		}
		//根据检查设备类型修改计数
		if(checkType==1){
			paramMap.put("checkType", 1);
			vehicleInfoMapper.updateCountById(paramMap);
		}else{
			if(personInfo.getPersonType()!=2){
				paramMap.put("checkType", 2);
				vehicleInfoMapper.updateCountById(paramMap);
			}
		}
		//添加历史记录
		DataRecordInfo dataRecordInfo=new DataRecordInfo();
		dataRecordInfo.setOrganizationInfoId(personInfo.getOrganizationInfoId());
		dataRecordInfo.setPersonName(personInfo.getName());
		dataRecordInfo.setCertificateCode(personInfo.getCertificateCode());
		dataRecordInfo.setBirthday(personInfo.getBirthday());
		dataRecordInfo.setGender(personInfo.getGender());
		dataRecordInfo.setNationality(personInfo.getNationality());
		dataRecordInfo.setAddress(personInfo.getAddress());
		dataRecordInfo.setSignOrganization(personInfo.getSignOrganization());
		dataRecordInfo.setCertificateCodeEffectiveDate(personInfo.getCertificateCodeEffectiveDate());
		dataRecordInfo.setCertificateFaceUrl(personInfo.getCertificateFaceUrl());
		dataRecordInfo.setPersonFaceUrl(personInfo.getPersonFaceUrl());
		dataRecordInfo.setPersonType(personInfo.getPersonType());
		dataRecordInfo.setVehicleLicense(vehicleInfo.getVehicleLicense());
		String longitude="";
		String latitude="";
		OrganizationInfo organizationInfo=organizationInfoMapper.selectByPrimaryKey(personInfo.getOrganizationInfoId());
		if(vehicleInfo.getVehicleType()!=0){
			TripInfo tripInfo=tripInfoMapper.findTripInfoByVehicleLicense(vehicleInfo.getVehicleLicense());
			//查看是否存在该车辆信息
    		PositionInfo tempPositionInfo=positionInfoMapper.getLastByTripId(tripInfo.getTripId());
    		if(tempPositionInfo!=null){
    			longitude=tempPositionInfo.getLongitude();
    			latitude=tempPositionInfo.getLatitude();
    			dataRecordInfo.setLongitud(longitude);
    			dataRecordInfo.setLatitude(latitude);
    		}
		}else{
			longitude=organizationInfo.getLongitude();
			latitude=organizationInfo.getLatitude();
			dataRecordInfo.setLongitud(longitude);
			dataRecordInfo.setLatitude(latitude);
		}
		dataRecordInfo.setCreateDate(new Date());
		if(checkType!=3){
			Map<String, String> paramCheckMap=new HashMap<String, String>();
			String sdkCheckPersonUrl=CommonUtil.getPropertiesParam("sdkCheckPersonUrl");
			String companyId=CommonUtil.getPropertiesParam("companyId");
			String ldqy=CommonUtil.getPropertiesParam("ldqy");
			String hpzl=CommonUtil.getPropertiesParam("hpzl");
			paramCheckMap.put("companyId", companyId);
			paramCheckMap.put("eventId", eventId);
			paramCheckMap.put("recordId", CommonUtil.getUUID());
			if(checkType==1){
				paramCheckMap.put("ldqy", ldqy);
				paramCheckMap.put("deptName", organizationInfo.getOrganizationName());
				paramCheckMap.put("hphm", vehicleInfo.getVehicleLicense());
				paramCheckMap.put("hpzl", hpzl);
			}
			CheckPersonInfo checkPersonInfo=new CheckPersonInfo();
			checkPersonInfo.setXm(personInfo.getName());
			checkPersonInfo.setXb(personInfo.getGender());
			checkPersonInfo.setMz(personInfo.getNationality());
			checkPersonInfo.setSfhm(personInfo.getCertificateCode());
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
			checkPersonInfo.setCsrq(sdf.format(personInfo.getBirthday()));
			checkPersonInfo.setSfzzz(personInfo.getAddress());
			checkPersonInfo.setYxqx(personInfo.getCertificateCodeEffectiveDate());
			checkPersonInfo.setQfjq(personInfo.getSignOrganization());
			if(!StringUtils.isEmpty(personInfo.getCertificateFaceUrl())){
				FTPClient ftpClient =null;
				InputStream is=null;
				try{
					ftpClient=FtpUtil.connect(CommonUtil.getPropertiesParam("ftp_address"), Integer.parseInt(CommonUtil.getPropertiesParam("ftp_port")), CommonUtil.getPropertiesParam("ftp_user"), CommonUtil.getPropertiesParam("ftp_password"));
					int changeWorkingLength = personInfo.getCertificateFaceUrl().split("/").length-1;
					if(changeWorkingLength>0){
						FtpUtil.changeWorkingDirectory(personInfo.getCertificateFaceUrl().substring(0, personInfo.getCertificateFaceUrl().lastIndexOf("/")), ftpClient);
						is=FtpUtil.retrieveFileStream(personInfo.getCertificateFaceUrl().substring(personInfo.getCertificateFaceUrl().lastIndexOf("/")+1,personInfo.getCertificateFaceUrl().length()), ftpClient);
					}else{
						is=FtpUtil.retrieveFileStream(personInfo.getCertificateFaceUrl(), ftpClient);
					}
					ByteArrayOutputStream output = new ByteArrayOutputStream();
				    byte[] buffer = new byte[4096];
				    int n = 0;
				    while (-1 != (n = is.read(buffer))) {
				        output.write(buffer, 0, n);
				    }
				    checkPersonInfo.setPic(output.toByteArray());
				}catch (Exception e) {
					logger.error("文件读取失败", e);
				}finally {
					try {
						FtpUtil.closeConnection(ftpClient);
					} catch (Exception e) {
						ftpClient=null;
					}
					try {
						if(is!=null){
							is.close();
						}
					} catch (Exception e) {
						is=null;
					}
				}
			}
			checkPersonInfo.setGpsX(latitude);
			checkPersonInfo.setGpsY(longitude);
			String checkPersonJson=JsonUtils.toJson(checkPersonInfo);
			logger.trace("省厅核查人员请求："+checkPersonJson);
			paramCheckMap.put("checkPersonInfo", checkPersonJson);
			String dataStr=HttpClientUtil.doPost(sdkCheckPersonUrl, paramCheckMap);
			logger.trace("省厅核查人员返回："+dataStr);
			if(!StringUtils.isEmpty(dataStr)){
				String code=JsonUtils.fromJson(dataStr).get("code").asText();
				if("000".equals(code)){
					CheckRecord checkRecord=new CheckRecord();
					checkRecord.setCheckType(2);
					checkRecord.setVehicleLicense(vehicleInfo.getVehicleLicense());
					checkRecord.setCheckCode(personInfo.getCertificateCode());
					checkRecord.setCheckReturnComment(dataStr);
					checkRecord.setCreateDate(new Date());
					checkRecord.setState(1);
					checkRecordMapper.insertSelective(checkRecord);
					Iterator<JsonNode> zbryList=JsonUtils.fromJson(dataStr).get("data").iterator();
					if(zbryList.hasNext()){
						AlertInfo alertInfo=new AlertInfo();
						alertInfo.setAlertType(2);
						alertInfo.setPersonInfoId(personInfo.getPersonInfoId());
						alertInfo.setAlertComment(JsonUtils.toJson(zbryList));
						alertInfo.setState(1);
						alertInfo.setCreateDate(new Date());
						Map<String, Object> paramMap3=new HashMap<String, Object>();
						paramMap3.put("personInfoId", personInfo.getPersonInfoId());
						paramMap3.put("alertType", 2);
						AlertInfo tempAlertInfo=alertInfoMapper.findAlertInfoByPersonIdAndAlertType(paramMap3);
						if(tempAlertInfo==null){
							alertInfoMapper.insertSelective(alertInfo);
						}else{
							alertInfo.setAlertInfoId(tempAlertInfo.getAlertInfoId());
							alertInfoMapper.updateByPrimaryKeySelective(alertInfo);
						}
						dataRecordInfo.setAlertInfoId(alertInfo.getAlertInfoId());
						dataRecordInfoMapper.insertSelective(dataRecordInfo);
						return alertInfo;
					}
				}
			}
//			Map<String, Object> map=new HashMap<String, Object>();
//			map.put("vehicleLicense", vehicleInfo.getVehicleLicense());
//			map.put("checkCode", personInfo.getCertificateCode());
//			CheckRecord checkRecord=checkRecordMapper.findCheckRecordByCheckCode(map);
//			if(checkRecord==null){
//				//调用省厅核查
//				String checkSingleUrl=CommonUtil.getPropertiesParam("checkSingleUrl");
//				String username=CommonUtil.getPropertiesParam("username");
//				String password=CommonUtil.getPropertiesParam("password");
//				String sbid=CommonUtil.getPropertiesParam("sbid");
//				Map<String, String> paramCheckMap=new HashMap<String, String>();
//				paramCheckMap.put("username", username);
//				paramCheckMap.put("password", password);
//				paramCheckMap.put("sbid", sbid);
//				paramCheckMap.put("sfzh", personInfo.getCertificateCode());
//				if(managerInfo!=null){
//					paramCheckMap.put("checkId", managerInfo.getCertificateCode());
//					paramCheckMap.put("checkName", managerInfo.getManagerName());
//				}else{
//					String checkName=CommonUtil.getPropertiesParam("checkName");
//					String checkId=CommonUtil.getPropertiesParam("checkId");
//					paramCheckMap.put("checkId", checkId);
//					paramCheckMap.put("checkName", checkName);
//				}
//				String checkJson=JsonUtils.toJson(paramCheckMap);
//				Map<String, String> postParamCheckMap=new HashMap<String, String>();
//				postParamCheckMap.put("jsonObj", checkJson);
//				String dataStr=HttpClientUtil.doPost(checkSingleUrl, postParamCheckMap);
//				logger.trace("省厅黑名单返回："+dataStr);
//				if(!StringUtils.isEmpty(dataStr)){
//					String code=JsonUtils.fromJson(dataStr).get("code").asText();
//					if("000".equals(code)){
//						checkRecord=new CheckRecord();
//						checkRecord.setCheckType(2);
//						checkRecord.setVehicleLicense(vehicleInfo.getVehicleLicense());
//						checkRecord.setCheckCode(personInfo.getCertificateCode());
//						checkRecord.setCheckReturnComment(dataStr);
//						checkRecord.setCreateDate(new Date());
//						checkRecord.setState(1);
//						checkRecordMapper.insertSelective(checkRecord);
//						Iterator<JsonNode> zbryList=JsonUtils.fromJson(dataStr).get("data").get("zbryList").iterator();
//						if(zbryList.hasNext()){
//							AlertInfo alertInfo=new AlertInfo();
//							alertInfo.setAlertType(2);
//							alertInfo.setPersonInfoId(personInfo.getPersonInfoId());
//							alertInfo.setAlertComment(JsonUtils.toJson(zbryList));
//							alertInfo.setState(1);
//							alertInfo.setCreateDate(new Date());
//							Map<String, Object> paramMap3=new HashMap<String, Object>();
//							paramMap3.put("personInfoId", personInfo.getPersonInfoId());
//							paramMap3.put("alertType", 2);
//							AlertInfo tempAlertInfo=alertInfoMapper.findAlertInfoByPersonIdAndAlertType(paramMap3);
//							if(tempAlertInfo==null){
//								alertInfoMapper.insertSelective(alertInfo);
//							}else{
//								alertInfo.setAlertInfoId(tempAlertInfo.getAlertInfoId());
//								alertInfoMapper.updateByPrimaryKeySelective(alertInfo);
//							}
//							dataRecordInfo.setAlertInfoId(alertInfo.getAlertInfoId());
//							dataRecordInfoMapper.insertSelective(dataRecordInfo);
//							return alertInfo;
//						}
//					}
//				}
//			}
			Map<String, Object> paramMap2=new HashMap<String, Object>();
			paramMap2.put("certificateCode", personInfo.getCertificateCode());
			Blacklist testBlacklist=blacklistMapper.findBlacklistByCertificateCode(paramMap2);
			//人员是否存在黑名单
			if(testBlacklist!=null){
				AlertInfo alertInfo=new AlertInfo();
				alertInfo.setAlertType(1);
				alertInfo.setPersonInfoId(personInfo.getPersonInfoId());
				alertInfo.setAlertComment(testBlacklist.getComment());
				alertInfo.setState(1);
				alertInfo.setCreateDate(new Date());
				Map<String, Object> paramMap3=new HashMap<String, Object>();
				paramMap3.put("personInfoId", personInfo.getPersonInfoId());
				paramMap3.put("alertType", 1);
				AlertInfo tempAlertInfo=alertInfoMapper.findAlertInfoByPersonIdAndAlertType(paramMap3);
				if(tempAlertInfo==null){
					alertInfoMapper.insertSelective(alertInfo);
				}else{
					alertInfo.setAlertInfoId(tempAlertInfo.getAlertInfoId());
					alertInfoMapper.updateByPrimaryKeySelective(alertInfo);
				}
				dataRecordInfo.setAlertInfoId(alertInfo.getAlertInfoId());
				dataRecordInfoMapper.insertSelective(dataRecordInfo);
				return alertInfo;
			}
		}
		dataRecordInfoMapper.insertSelective(dataRecordInfo);
		return null;
	}

	@Override
	public PersonInfo findPersonInfoByCertificateCode(String certificateCode) {
		
		return personInfoMapper.findPersonInfoByCertificateCode(certificateCode);
	}

	@Override
	public List<PersonInfo> findPersonInfoByVehicleLicense(String vehicleLicense) {
		
		return personInfoMapper.findPersonInfoByVehicleLicense(vehicleLicense);
	}

	@Override
	public Map<String, Object> findPersonInfoAndAlertPageByVehicleLicense(String vehicleLicense, int currentPage,
			int pageSize) {
		Map<String, Object> dataMap=new HashMap<String, Object>();
		Map<String, Object> paramMap=new HashMap<String, Object>();
		paramMap.put("pageSize", pageSize);
		paramMap.put("startRow", (currentPage - 1)*pageSize);
		paramMap.put("vehicleLicense", vehicleLicense);
		List<Map<String, Object>> dataList=personInfoMapper.findPersonInfoAndAlertPage(paramMap);
		int totalCount=personInfoMapper.findPersonInfoAndAlertCount(paramMap);
		dataMap.put("totalCount", totalCount);
		if(totalCount%pageSize!=0){
			dataMap.put("totalPage", totalCount/pageSize+1);
		}else{
			dataMap.put("totalPage", totalCount/pageSize);
		}
		
		dataMap.put("dataList", dataList);
		return dataMap;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public AlertInfo supplementPerson(PersonInfo personInfo, VehicleInfo vehicleInfo,String certificateCode,ManagerInfo managerInfo,String eventId) {
		AlertInfo alertInfo=null;
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("vehicleInfoId", vehicleInfo.getVehicleInfoId());
		paramMap.put("checkType", 4);
		vehicleInfoMapper.updateCountById(paramMap);
		PersonInfo tempPersonInfo=personInfoMapper.findPersonInfoByCertificateCode(personInfo.getCertificateCode());
		Map<String, Object> dataMap=new HashMap<String, Object>();
		
//		Map<String, Object> map=new HashMap<String, Object>();
//		map.put("vehicleLicense", vehicleInfo.getVehicleLicense());
//		map.put("checkCode", personInfo.getCertificateCode());
//		CheckRecord checkRecord=checkRecordMapper.findCheckRecordByCheckCode(map);
//		if(checkRecord==null){
//			//调用省厅核查
//			String checkSingleUrl=CommonUtil.getPropertiesParam("checkSingleUrl");
//			String username=CommonUtil.getPropertiesParam("username");
//			String password=CommonUtil.getPropertiesParam("password");
//			String sbid=CommonUtil.getPropertiesParam("sbid");
//			Map<String, String> paramCheckMap=new HashMap<String, String>();
//			paramCheckMap.put("username", username);
//			paramCheckMap.put("password", password);
//			paramCheckMap.put("sbid", sbid);
//			paramCheckMap.put("sfzh", personInfo.getCertificateCode());
//			paramCheckMap.put("checkId", managerInfo.getCertificateCode());
//			paramCheckMap.put("checkName", managerInfo.getManagerName());
//			String checkJson=JsonUtils.toJson(paramCheckMap);
//			Map<String, String> postParamCheckMap=new HashMap<String, String>();
//			postParamCheckMap.put("jsonObj", checkJson);
//			String dataStr=HttpClientUtil.doPost(checkSingleUrl, postParamCheckMap);
//			logger.trace("省厅黑名单返回："+dataStr);
//			if(!StringUtils.isEmpty(dataStr)){
//				String code=JsonUtils.fromJson(dataStr).get("code").asText();
//				if("000".equals(code)){
//					checkRecord=new CheckRecord();
//					checkRecord.setCheckType(2);
//					checkRecord.setVehicleLicense(vehicleInfo.getVehicleLicense());
//					checkRecord.setCheckCode(personInfo.getCertificateCode());
//					checkRecord.setCheckReturnComment(dataStr);
//					checkRecord.setCreateDate(new Date());
//					checkRecord.setState(1);
//					checkRecordMapper.insertSelective(checkRecord);
//					Iterator<JsonNode> zbryList=JsonUtils.fromJson(dataStr).get("data").get("zbryList").iterator();
//					if(zbryList.hasNext()){
//						alertInfo=new AlertInfo();
//						alertInfo.setAlertType(2);
//						alertInfo.setPersonInfoId(personInfo.getPersonInfoId());
//						alertInfo.setAlertComment(JsonUtils.toJson(zbryList));
//						alertInfo.setState(1);
//						alertInfo.setCreateDate(new Date());
//						Map<String, Object> paramMap3=new HashMap<String, Object>();
//						paramMap3.put("personInfoId", personInfo.getPersonInfoId());
//						paramMap3.put("alertType", 2);
//						AlertInfo tempAlertInfo=alertInfoMapper.findAlertInfoByPersonIdAndAlertType(paramMap3);
//						if(tempAlertInfo==null){
//							alertInfoMapper.insertSelective(alertInfo);
//						}else{
//							alertInfo.setAlertInfoId(tempAlertInfo.getAlertInfoId());
//							alertInfoMapper.updateByPrimaryKeySelective(alertInfo);
//						}
//						dataMap.put("alertInfoId",alertInfo.getAlertInfoId());
//					}
//				}
//			}
//		}
		if(tempPersonInfo==null){
			personInfoMapper.updateByPrimaryKeySelective(personInfo);
			//省厅核查人员
			OrganizationInfo organizationInfo=organizationInfoMapper.selectByPrimaryKey(managerInfo.getOrganizationInfoId());
			Map<String, String> paramCheckMap=new HashMap<String, String>();
			String sdkCheckPersonUrl=CommonUtil.getPropertiesParam("sdkCheckPersonUrl");
			String companyId=CommonUtil.getPropertiesParam("companyId");
			String ldqy=CommonUtil.getPropertiesParam("ldqy");
			String hpzl=CommonUtil.getPropertiesParam("hpzl");
			paramCheckMap.put("companyId", companyId);
			paramCheckMap.put("eventId", eventId);
			paramCheckMap.put("recordId", CommonUtil.getUUID());
			paramCheckMap.put("ldqy", ldqy);
			paramCheckMap.put("deptName", organizationInfo.getOrganizationName());
			paramCheckMap.put("hphm", vehicleInfo.getVehicleLicense());
			paramCheckMap.put("hpzl", hpzl);
			CheckPersonInfo checkPersonInfo=new CheckPersonInfo();
			checkPersonInfo.setXm(personInfo.getName());
			checkPersonInfo.setXb(personInfo.getGender());
			checkPersonInfo.setMz(personInfo.getNationality());
			checkPersonInfo.setSfhm(personInfo.getCertificateCode());
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
			if(personInfo.getBirthday()!=null){
				checkPersonInfo.setCsrq(sdf.format(personInfo.getBirthday()));
			}
			checkPersonInfo.setSfzzz(personInfo.getAddress());
			checkPersonInfo.setYxqx(personInfo.getCertificateCodeEffectiveDate());
			checkPersonInfo.setQfjq(personInfo.getSignOrganization());
			String pic=null;
			if(!StringUtils.isEmpty(personInfo.getCertificateFaceUrl())){
				pic=personInfo.getCertificateFaceUrl();
			}else{
				pic=personInfo.getPersonFaceUrl();
			}
			if(!StringUtils.isEmpty(pic)){
				FTPClient ftpClient =null;
				InputStream is=null;
				try{
					ftpClient=FtpUtil.connect(CommonUtil.getPropertiesParam("ftp_address"), Integer.parseInt(CommonUtil.getPropertiesParam("ftp_port")), CommonUtil.getPropertiesParam("ftp_user"), CommonUtil.getPropertiesParam("ftp_password"));
					int changeWorkingLength = pic.split("/").length-1;
					if(changeWorkingLength>0){
						FtpUtil.changeWorkingDirectory(personInfo.getCertificateFaceUrl().substring(0, personInfo.getCertificateFaceUrl().lastIndexOf("/")), ftpClient);
						is=FtpUtil.retrieveFileStream(personInfo.getCertificateFaceUrl().substring(personInfo.getCertificateFaceUrl().lastIndexOf("/")+1,personInfo.getCertificateFaceUrl().length()), ftpClient);
					}else{
						is=FtpUtil.retrieveFileStream(personInfo.getCertificateFaceUrl(), ftpClient);
					}
					ByteArrayOutputStream output = new ByteArrayOutputStream();
				    byte[] buffer = new byte[4096];
				    int n = 0;
				    while (-1 != (n = is.read(buffer))) {
				        output.write(buffer, 0, n);
				    }
				    checkPersonInfo.setPic(output.toByteArray());
				}catch (Exception e) {
					logger.error("文件读取失败", e);
				}finally {
					try {
						FtpUtil.closeConnection(ftpClient);
					} catch (Exception e) {
						ftpClient=null;
					}
					try {
						if(is!=null){
							is.close();
						}
					} catch (Exception e) {
						is=null;
					}
				}
			}
			checkPersonInfo.setGpsX(organizationInfo.getLatitude());
			checkPersonInfo.setGpsY(organizationInfo.getLongitude());
			String checkPersonJson=JsonUtils.toJson(checkPersonInfo);
			logger.trace("省厅核查人员请求："+checkPersonJson);
			paramCheckMap.put("checkPersonInfo", checkPersonJson);
			String dataStr=HttpClientUtil.doPost(sdkCheckPersonUrl, paramCheckMap);
			logger.trace("省厅核查人员返回："+dataStr);
			if(!StringUtils.isEmpty(dataStr)){
				String code=JsonUtils.fromJson(dataStr).get("code").asText();
				if("000".equals(code)){
					CheckRecord checkRecord=new CheckRecord();
					checkRecord.setCheckType(2);
					checkRecord.setVehicleLicense(vehicleInfo.getVehicleLicense());
					checkRecord.setCheckCode(personInfo.getCertificateCode());
					checkRecord.setCheckReturnComment(dataStr);
					checkRecord.setCreateDate(new Date());
					checkRecord.setState(1);
					checkRecordMapper.insertSelective(checkRecord);
					Iterator<JsonNode> zbryList=JsonUtils.fromJson(dataStr).get("data").iterator();
					if(zbryList.hasNext()){
						alertInfo=new AlertInfo();
						alertInfo.setAlertType(2);
						alertInfo.setPersonInfoId(personInfo.getPersonInfoId());
						alertInfo.setAlertComment(JsonUtils.toJson(zbryList));
						alertInfo.setState(1);
						alertInfo.setCreateDate(new Date());
						Map<String, Object> paramMap3=new HashMap<String, Object>();
						paramMap3.put("personInfoId", personInfo.getPersonInfoId());
						paramMap3.put("alertType", 2);
						AlertInfo tempAlertInfo=alertInfoMapper.findAlertInfoByPersonIdAndAlertType(paramMap3);
						if(tempAlertInfo==null){
							alertInfoMapper.insertSelective(alertInfo);
						}else{
							alertInfo.setAlertInfoId(tempAlertInfo.getAlertInfoId());
							alertInfoMapper.updateByPrimaryKeySelective(alertInfo);
						}
						dataMap.put("alertInfoId",alertInfo.getAlertInfoId());
					}else{
						Map<String, Object> paramMap2=new HashMap<String, Object>();
						paramMap2.put("certificateCode", personInfo.getCertificateCode());
						Blacklist testBlacklist=blacklistMapper.findBlacklistByCertificateCode(paramMap2);
						//人员是否存在黑名单
						if(testBlacklist!=null){
							alertInfo=new AlertInfo();
							alertInfo.setAlertType(1);
							alertInfo.setPersonInfoId(personInfo.getPersonInfoId());
							alertInfo.setAlertComment(testBlacklist.getComment());
							alertInfo.setState(1);
							alertInfo.setCreateDate(new Date());
							Map<String, Object> paramMap3=new HashMap<String, Object>();
							paramMap3.put("personInfoId", personInfo.getPersonInfoId());
							paramMap3.put("alertType", 1);
							AlertInfo tempAlertInfo=alertInfoMapper.findAlertInfoByPersonIdAndAlertType(paramMap3);
							if(tempAlertInfo==null){
								alertInfoMapper.insertSelective(alertInfo);
							}else{
								alertInfo.setAlertInfoId(tempAlertInfo.getAlertInfoId());
								alertInfoMapper.updateByPrimaryKeySelective(alertInfo);
							}
							dataMap.put("alertInfoId",alertInfo.getAlertInfoId());
						}
					}
				}
			}
			
			dataMap.put("personName", personInfo.getName());
			dataMap.put("certificateCode", personInfo.getCertificateCode());
			dataMap.put("birthday", personInfo.getBirthday());
			dataMap.put("gender", personInfo.getGender());
			dataMap.put("nationality", personInfo.getNationality());
			dataMap.put("address", personInfo.getAddress());
			dataMap.put("signOrganization", personInfo.getSignOrganization());
			dataMap.put("certificateCodeEffectiveDate", personInfo.getCertificateCodeEffectiveDate());
			dataMap.put("certificateFaceUrl", personInfo.getCertificateFaceUrl());
			dataMap.put("personType", personInfo.getPersonType());
			dataMap.put("oldCertificateCode", certificateCode);
			dataRecordInfoMapper.updateDataRecordByCertificateCode(dataMap);
		}else{
			personInfoMapper.deleteByPrimaryKey(personInfo.getPersonInfoId());
			dataRecordInfoMapper.delDataRecordByCertificateCode(certificateCode);
			if(!tempPersonInfo.getVehicleInfoId().equals(vehicleInfo.getVehicleInfoId())){
				personInfo.setPersonInfoId(tempPersonInfo.getPersonInfoId());
				personInfo.setVehicleInfoId(vehicleInfo.getVehicleInfoId());
				personInfoMapper.updateByPrimaryKeySelective(personInfo);
			}
			Map<String, Object> paramMap3=new HashMap<String, Object>();
			paramMap3.put("personInfoId", personInfo.getPersonInfoId());
			paramMap3.put("alertType", 1);
			alertInfo=alertInfoMapper.findAlertInfoByPersonIdAndAlertType(paramMap3);
		}
		return alertInfo;
	}
}